package com.pcdandan.pcdandan.processor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.pcdandan.pcdandan.entity.XiazhuInfo;
import com.pcdandan.pcdandan.service.PcdandanService;
import com.youyu.financial.gjj.crawler.annotation.Process;
import com.youyu.financial.gjj.crawler.executor.Context;
import com.youyu.financial.gjj.crawler.processor.PageProcessor;
import com.youyu.financial.gjj.crawler.visitor.Visitor;
import com.youyu.financial.gjj.crawler.visitor.http.HttpRequest;

@Process(method = "auto", domain = "pcdandan")
public class PcdandanProcessor implements PageProcessor {
	
	private static String pcdandanUrl = "http://www.pceggs.com/play/28zs.aspx";
    private static String nowdanUrl = "http://www.pceggs.com/play/pxya.aspx";
    private static int length = 6;
    private static Set<String> pcdandanroles = new HashSet();
    private static Map<String, Integer> pcdandanrolesMap = new HashMap();
    private static Map<String, Integer> displayMap = new HashMap();
    private static Map<String, Integer> intervalMap = new HashMap();
    private static int[] base = new int[]{0, 1};
    private static int changdu = 4;
    
    private static String mySession = ".ADWASPX7A5C561934E_PCEGGS";
    
    private static Visitor visitor;
    private static Context context;

	@Override
	public XiazhuInfo process(Visitor v, Context c) {
		visitor = v;
		context = c;
		xiazhu();
		return null;
	}
	
	
	public static List<String> zuhe(int length) {
        ArrayList zuhes = new ArrayList();
        fenlie(length, zuhes, "");
        return zuhes;
    }

    private static void fenlie(int length, List<String> zuhes, String now) {
        if(length == 0) {
            zuhes.add(now);
            now = "";
        } else {
            for(int i = 0; i < base.length; ++i) {
                String newnow = now + base[i];
                fenlie(length - 1, zuhes, newnow);
            }
        }

    }

    public static String xiazhu() {

        String content = "";

        try {
            content = getRemoteContent(pcdandanUrl);
        } catch (Exception var19) {
            var19.printStackTrace();
        }

        Document moduleDoc = Jsoup.parse(content);
        Elements tables = moduleDoc.select("table");
        Elements trs = tables.get(1).select("tr");
        Iterator tr = trs.iterator();
        int i = 199;
        int[] results = new int[200];

        while(tr.hasNext()) {
            Element over = (Element)tr.next();
            String len = over.select(".shuang").text();
            String all = over.select(".dan").text();
            if("单".equals(all.trim())) {
                results[i] = 1;
                --i;
            } else if("双".equals(len.trim())) {
                results[i] = 0;
                --i;
            }
        }

        boolean var20 = false;
        int var21 = results.length;

        int l;
        for(int var22 = 0; var22 < var21; ++var22) {
            String outside = String.valueOf(results[var22]);

            for(int lastSix = 1; lastSix < length; ++lastSix) {
                if(var22 + lastSix > 199) {
                    var20 = true;
                    break;
                }

                outside = outside + results[var22 + lastSix];
            }

            if(var20) {
                break;
            }

            pcdandanroles.add(outside);
            if(displayMap.get(outside) == null) {
                displayMap.put(outside, Integer.valueOf(var22));
            } else {
                Integer var25 = (Integer)intervalMap.get(outside);
                l = ((Integer)displayMap.get(outside)).intValue();
                if(var25 == null || var22 - l < var25.intValue()) {
                    intervalMap.put(outside, Integer.valueOf(var22 - l));
                }
            }

            pcdandanrolesMap.put(outside, Integer.valueOf(valueOf(pcdandanrolesMap.get(outside)) + 1));
        }

        List var23 = zuhe(length);
        List var24 = filter(var23, pcdandanroles);
        Iterator var27 = var24.iterator();

        String var26;
        while(var27.hasNext()) {
            var26 = (String)var27.next();
            System.out.println("不存在的排列：" + var26);
        }

        System.out.println(var24.size());
        var26 = last6();
        l = changdu;
        var26 = var26.substring(6 - l);
        Iterator var15 = pcdandanrolesMap.keySet().iterator();

        while(true) {
            String key;
            do {
                do {
                    if(!var15.hasNext()) {
                        return "";
                    }

                    key = (String)var15.next();
                } while(((Integer)pcdandanrolesMap.get(key)).intValue() >= 3);
            } while(key.indexOf(var26) != 0);

            System.out.println(key + ":" + pcdandanrolesMap.get(key));
            System.out.println("最近一次出现位置：" + displayMap.get(key));
            System.out.println("最近一次连续出现间隔：" + intervalMap.get(key));
            String last = key.substring(l);
            String newLast = "";

            for(int j = 0; j < last.length(); ++j) {
                if(last.charAt(j) == 49) {
                    newLast = newLast + "0";
                } else {
                    newLast = newLast + "1";
                }
            }

            System.out.println("后续可购买" + newLast);
        }
    }

    private static String last6(){
        String html = getRemoteContent(nowdanUrl);
        Document d = Jsoup.parse(html);
        Elements table = d.select("#panel");
        Elements trs = table.select("tr");
        Iterator tr = trs.iterator();
        String nums = "";
        int i = 0;

        while(tr.hasNext()) {
            Element ele = (Element)tr.next();
            String text = ele.select("td").eq(2).select("script").html();
            int index = text.indexOf("\');");
            if(index > 5) {
                String num = text.substring(index - 2, index);
                if(num.equals("-1")) {
                    continue;
                }

                if(Integer.valueOf(num).intValue() % 2 == 0) {
                    nums = "0" + nums;
                } else {
                    nums = "1" + nums;
                }

                ++i;
            }

            if(i == 6) {
                break;
            }
        }

        //System.out.println("最近的6次:" + nums);
        return nums;
    }

    private static String getRemoteContent(String nowdanUrl2) {
		return visitor.visit(HttpRequest.newBuilder().cookie(mySession, PcdandanService.session).get(nowdanUrl2).build()).page().html();
	}


	private static List<String> filter(List<String> all, Set<String> pcdandanroles2) {
        ArrayList canWins = new ArrayList();
        Iterator var4 = all.iterator();

        while(var4.hasNext()) {
            String one = (String)var4.next();
            if(!pcdandanroles2.contains(one)) {
                canWins.add(one);
            }
        }

        return canWins;
    }

    private static int valueOf(Object o) {
        return o == null?0:Integer.valueOf(o.toString()).intValue();
    }

}
